#include "qusermanager.h"

#include <QVariant>
#include <QFile>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>

using namespace SHAREDLIB;

QUserManager::QUserManager(QObject *parent) :
    QObject(parent),
    m_currentUser(NULL)
{
}

QUserManager::~QUserManager()
{
    qDeleteAll(m_userList);
    m_userList.clear();
}

void QUserManager::addUser(QObject *obj)
{
    if(obj==NULL || m_userList.contains(obj))
    {
        return;
    }

    m_userList.append(obj);
    emit userAdd(obj,-1);
}

void QUserManager::inserUser(int index, QObject *obj)
{
    if(obj==NULL || m_userList.contains(obj))
    {
        return;
    }
    if(index<0)
    {
        index=0;
    }
    if(index>=m_userList.size())
    {
        index=m_userList.size();
    }
    m_userList.insert(index,obj);
    emit userAdd(obj,index);
}

void QUserManager::removeUser(const QString &userName)
{
    foreach(QObject* obj,m_userList)
    {
        if(obj->property("userName").toString()==userName)
        {
            m_userList.removeAll(obj);
            emit userDel(obj);
            delete obj;
            return;
        }
    }
}

QObject* QUserManager::getUser(int index)
{
    if(index<0 || index>=m_userList.size())
    {
        return NULL;
    }
    return m_userList.at(index);
}

QObject* QUserManager::getUser(const QString &userName)
{
    foreach(QObject* obj,m_userList)
    {
        if(obj->property("userName").toString()==userName)
        {
            return obj;
        }
    }
    return NULL;
}

void QUserManager::swapUser(int index1, int index2)
{
    m_userList.swap(index1,index2);
}

void QUserManager::setCurrentUser(const QString &userName)
{
    foreach(QObject* obj,m_userList)
    {
        if(obj->property("userName").toString()==userName)
        {
            if(m_currentUser!=obj)
            {
                m_currentUser=obj;
                emit currentUserChanged(obj);
                return;
            }
        }
    }
    if(m_currentUser!=NULL)
    {
        m_currentUser=NULL;
        emit currentUserChanged(NULL);
    }
}

void QUserManager::setCurrentUser(QObject *obj)
{
    if(m_currentUser!=obj)
    {
        m_currentUser=obj;
        emit currentUserChanged(m_currentUser);
    }
}

QObject* QUserManager::getCurrentUser()
{
    return m_currentUser;
}

int QUserManager::userIndex(QObject *obj)
{
    return m_userList.indexOf(obj);
}

int QUserManager::userCount()
{
    return m_userList.size();
}

void QUserManager::save(const QString &projectPath)
{
    QString filename=projectPath+"/user.xml";
    QFile f(filename);
    if(f.open(QFile::WriteOnly))
    {
        f.resize(0);
        QXmlStreamWriter r(&f);
        r.setAutoFormatting(true);

        r.writeStartDocument();
        r.writeStartElement("Users");
        foreach(QObject* obj,m_userList)
        {
            r.writeStartElement("User");

            r.writeAttribute("userName",obj->property("userName").toString());
            r.writeAttribute("passWord",obj->property("passWord").toString());
            r.writeAttribute("level",obj->property("level").toString());
            r.writeAttribute("explanation",obj->property("explanation").toString());

            r.writeEndElement();
        }

        r.writeEndElement();
        r.writeEndDocument();

    }
}

void QUserManager::load(const QString &projectPath)
{
    release();
    QString filename=projectPath+"/user.xml";
    QFile f(filename);
    if(!f.exists())
    {
        save(projectPath);
        return;
    }
    if(f.open(QFile::ReadOnly))
    {
        QXmlStreamReader r(&f);

        while(!r.atEnd())
        {
            if(r.readNext()==QXmlStreamReader::StartElement)
            {
                if(r.name().toString()=="User")
                {
                    loadUser(&r);
                }
            }
        }
        if(r.hasError())
        {
            qDebug(r.errorString().toLocal8Bit());
            release();
        }
    }
}

void QUserManager::loadUser(QXmlStreamReader *r)
{
    QString userName;
    QString passWord;
    QString level;
    QString explanation;
    foreach(const QXmlStreamAttribute &attribute,r->attributes())
    {
        if(attribute.name().toString()=="userName")
        {
            userName=attribute.value().toString();
        }
        else if(attribute.name().toString()=="passWord")
        {
            passWord=attribute.value().toString();
        }
        else if(attribute.name().toString()=="level")
        {
            level=attribute.value().toString();
        }
        else if(attribute.name().toString()=="explanation")
        {
            explanation=attribute.value().toString();
        }
    }
    if(userName=="" || passWord=="" || level.toInt()<=0 || level.toInt()>100)
    {
        r->raiseError("User Error!");
        return;
    }
    QObject *obj=new QObject;
    obj->setProperty("userName",userName);
    obj->setProperty("passWord",passWord);
    obj->setProperty("level",level);
    obj->setProperty("explanation",explanation);
    addUser(obj);
}

void QUserManager::release()
{
    m_currentUser=NULL;
    qDeleteAll(m_userList);
    m_userList.clear();
}

void QUserManager::setUserName(QObject *obj, const QString &now)
{
    QString str=obj->property("userName").toString();
    if(str!=now)
    {
        obj->setProperty("userName",now);
        emit userNameChanged(str,now);
    }
}
